package xyz.kongbai121.demo.util;

import redis.clients.jedis.Jedis;

import java.io.File;
import java.nio.file.Files;

/**
 * 过期图片删除工具类
 */
public class OverImagesDeleteUtil {

    //记录删除图片的数量
    private static Integer OVER_NUM = 0;

    private static Jedis jedis = new Jedis("localhost",6379);


    public static String imagesDelete(String redisKey){
        String img = jedis.lpop(redisKey);
        if (img != null && !img.isEmpty()){
            //获取文件名
            String fileName = img.substring(img.lastIndexOf("/")+1);
            System.out.println("删除"+fileName);
            try {
                //TODO 删除文件
                File file = new File("../userdata/" + fileName);
                Files.delete(file.toPath());
                OVER_NUM++;
            }catch (Exception e){
                e.printStackTrace();
            }
            OverImagesDeleteUtil.imagesDelete(redisKey);
        }
        return "一共删除了" + OVER_NUM + "张图片";
    }

    public static  void  resetOverNum(){
        OVER_NUM = 0;
    }
}
